package logging

import (
	"crypto/tls"
	"fmt"
	"net/http"
	"v8s-controller/internal/configuration"

	"github.com/opensearch-project/opensearch-go"
)

func GetLoggingApiClient(properties configuration.LoggingProperties) *opensearch.Client {
	// Initialize the client with SSL/TLS enabled.
	client, err := opensearch.NewClient(opensearch.Config{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		},
		Addresses: []string{properties.Host},
		Username:  properties.User, // For testing only. Don't store credentials in code.
		Password:  properties.Password,
	})

	if err != nil {
		panic(fmt.Errorf("could not establish connect to host '%s': %s", properties.Host, err))
	}
	return client

}

func GetLoggingDashboardApiClient(properties configuration.LoggingProperties, username, password string) *opensearch.Client {
	client, err := opensearch.NewClient(opensearch.Config{
		Transport:            &http.Transport{},
		Addresses:            []string{properties.DashboardHost},
		UseResponseCheckOnly: true,
		Username:             username,
		Password:             password,
	})
	if err != nil {
		panic(fmt.Errorf("could not establish connect to host '%s': %s", properties.DashboardHost, err))
	}
	return client

}
